package org.theonept.travian.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.theonept.travian.util.HttpUtil;

public class Village extends Entity {
	private String nomVillage;

	private String idVillage;
	private String positionX;
	private String positionY;
	private Production production;
	private Compte compte;
	private Boolean constructionEnCours;

	public String getNomVillage() {
		return nomVillage;
	}

	public void setNomVillage(String nomVillage) {
		this.nomVillage = nomVillage;
	}

	public String getIdVillage() {
		return idVillage;
	}

	public void setIdVillage(String idVillage) {
		this.idVillage = idVillage;
	}

	public Production getProduction() {
		return getProduction(production == null || !production.isUpToDate());
	}

	public Production getProduction(boolean forceUpdate) {
		if (forceUpdate) {
			production = Production.getProduction(this);
			production.update();
		}
		return production;
	}

	public void setProduction(Production production) {

		this.production = production;
	}

	public static List<Village> getVillages(String url, Compte compte) {
		List<Village> villages = new ArrayList<Village>();
		Document document = HttpUtil.getDocumentFromUrl(url);
		List<Element> activeElement = document.getElementById(
				"sidebarBoxVillagelist").getElementsByAttributeValueContaining(
				"href", "?newdid=");
		for (Element element : activeElement) {
			Village village = new Village();
			village.setNomVillage(element
					.getElementsByAttributeValue("class", "name").first()
					.text());
			village.setIdVillage(element.attr("href").replace("?newdid=", ""));
			village.setPositionX(element
					.getElementsByAttributeValue("class", "coordinateX")
					.first().text().replace("(", ""));
			village.setPositionY(element
					.getElementsByAttributeValue("class", "coordinateY")
					.first().text().replace(")", ""));
			village.setCompte(compte);

			villages.add(village);

		}

		return villages;
	}

	public String getPositionX() {
		return positionX;
	}

	public void setPositionX(String positionX) {
		this.positionX = positionX;
	}

	public String getPositionY() {
		return positionY;
	}

	public void setPositionY(String positionY) {
		this.positionY = positionY;
	}

	public Compte getCompte() {
		return compte;
	}

	public void setCompte(Compte compte) {
		this.compte = compte;
	}

	/**
	 * @return the constructionEnCours
	 */
	public Boolean getConstructionEnCours() {

		Document documentVillage = HttpUtil.getDocumentFromUrl(getCompte()
				.getUrlServeur() + "dorf1.php?newdid=" + getIdVillage());
		return !(documentVillage.getElementById("content")
				.getElementsByAttributeValue("class", "buildDuration")
				.isEmpty());
	}

	/**
	 * @param constructionEnCours
	 *            the constructionEnCours to set
	 */
	public void setConstructionEnCours(Boolean constructionEnCours) {
		this.constructionEnCours = constructionEnCours;
	}

	public void upRessourceAuto() {

		if (!getConstructionEnCours()) {
			List<Ressource> ressources = getProduction().getRessources();
			Collections.sort(ressources, new ComparatorRessourceUp());
			for (Ressource ressource : ressources) {
				if (ressource.isUpable()) {
					ressource.up();
					break;
				}
			}
		}

	}
}
